package com.exam.system.service.impl;

import com.exam.system.dao.ExamLibraryTypeDao;
import com.exam.system.entity.ExamLibrary;
import com.exam.system.entity.ExamLibraryType;
import com.exam.system.entity.PageDTO;
import com.exam.system.service.ExamLibraryTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("examLibraryTypeService")
public class ExamLibraryTypeServiceImpl implements ExamLibraryTypeService {
    @Autowired
    ExamLibraryTypeDao examLibraryTypeDao;

    @Override
    public List<ExamLibraryType> findAllExamLibraryType() {
       return examLibraryTypeDao.findAllExamLibraryType();
    }

    @Override
    public List<ExamLibraryType> findchirdExamLibraryType() {
        return examLibraryTypeDao.findchirdExamLibraryType();
    }

    //修改题库类型
    @Override
    public Integer updateLibraryType(ExamLibraryType examLibraryType) {
        return examLibraryTypeDao.updateLibraryType(examLibraryType);
    }

    //查询所有题库类型并分页
    @Override
    public List<ExamLibraryType> findAllExamLibraryTypeList() {
        List<ExamLibraryType> list = new ArrayList();
        List<ExamLibraryType> examLibraryTypeList = examLibraryTypeDao.findAllExamLibraryType();
        for (int i = 0; i < examLibraryTypeList.size(); i++) {
            if (examLibraryTypeList.get(i).getIsparent().equals("y")){
                List<ExamLibraryType>  chirdlist = new ArrayList();
                for (int j = 0; j < examLibraryTypeList.size(); j++) {
                    if ( examLibraryTypeList.get(i).getId()==examLibraryTypeList.get(j).getParentId()){
                        chirdlist.add(examLibraryTypeList.get(j));
                    }
                }
                examLibraryTypeList.get(i).setChildlibraryType(chirdlist);
                list.add(examLibraryTypeList.get(i));
            }
        }
        return list;
    }

    //添加父题库类型下的子题库类型
    @Override
    public Integer addChildLibraryType(ExamLibraryType examLibraryType) {
        return examLibraryTypeDao.addChildLibraryType(examLibraryType);
    }

    @Override
    public Integer addParentLibraryType(ExamLibraryType examLibraryType) {
        return examLibraryTypeDao.addParentLibraryType(examLibraryType);
    }

    @Override
    public Integer disableLibraryType(ExamLibraryType ExamLibraryType) {
        return examLibraryTypeDao.disableLibraryType(ExamLibraryType);
    }
}
